Prevent incorrect clipping of text.#238
Conversation
When the text bounding box had a negative y0, the text was cropped. This happened for all vertical alignments except ascender, notably for baseline, which is needed for aligning text of different sizes. The new implementation is simpler and more obviously correct, as all conditions on the sign of x0 and y0 were removed. Also optimize the size of cached images when x0 or y0 is positive by saving exactly the bounding box, without blank space on the top left.
|
I managed to run the tests locally but I'm getting differences on all screenshots, possibly because I'm on MacOS or because I don't have the exact same fonts as you. I checked on a basic case that the differences look the same with and without my patch. I believe, but couldn't confirm that adding the following test and generating the corresponding PNG should do the job. Without my patch, I expect the text to be cropped: only the bottom of letters below the below the baseline will be visible. With my patch, the text should be fully visible. diff --git a/tests/widgets/test_widgets.py b/tests/widgets/test_widgets.py
index 8799288..97ffff4 100644
--- a/tests/widgets/test_widgets.py
+++ b/tests/widgets/test_widgets.py
@@ -35,6 +35,11 @@ def test_render_text():
return time_rendering("simple text", [Text(Coordinate(50, 50), lambda: "Hello", font)])
+@pytest.mark.gfx
+@approve_image
+def test_render_text_baseline():
+ return time_rendering("simple text", [Text(Coordinate(50, 50), lambda: "Hello", font, align="ms")])
+
+
@pytest.mark.gfx
@approve_image
def test_render_text_colour(): |
|
NB: this looks like a straightforward issue and fix to me. Using baseline alignment on any text results in the text getting cropped. I'm happy to create an issue if further discussion is needed though. |
|
Finally, if you want a less intrusive fix, replacing I prefer the version I'm proposing with all the conditional branches, including the |
When the text bounding box had a negative y0, the text was cropped. This happened for all vertical alignments except ascender, notably for baseline, which is needed for aligning text of different sizes.
The new implementation is simpler and more obviously correct, as all conditions on the sign of x0 and y0 were removed.
Also optimize the size of cached images when x0 or y0 is positive by saving exactly the bounding box, without blank space on the top left.